<?php
class ImportBkXML {
    public function load($file) {
        $dom = new DOMDocument();
        $dom -> load($file);
        $xpath = new DOMXpath($dom);
        $list = $xpath -> query("//body/div1");
        $section_id = '';

        if ($list -> length > 0) {
            $div1 = $list -> item(0);
            $type = $div1 -> getAttribute('type');
            if ($type == 'section') {
                $section_id = $div1 -> getAttribute('xml:id');
            }
        }

        //import bk*.xml file
        $letter = D('Letter');
        $div2 = $xpath -> query("//div2");
        foreach ($div2 as $elem) {
            $data = array();
            $data['section_id'] = $section_id;
            $data['letter_id'] = $elem -> getAttribute('xml:id');
            $data = $this -> readNode($elem, $data);
            $letter -> add($this->trimData($data));
        }
    } 

    private function getChildNode($node, $cname, $ctype) {
        $cList = $node -> childNodes;
        foreach ($cList as $c) {
            if ($c -> tagName == $cname && $c -> getAttribute('type') == $ctype) {
                return $c;
            }
        }
        return null;
    }

    /**
     * Insert name into table "absender" and "adressat"
     */
    private function addToTable(DOMElement $elem, $letter_id, $talbeName) {
        $cList = $elem -> childNodes;
        $firstName = '';
        $lastName = null;
        $fullName = $elem -> textContent;
        $model = D($talbeName);

        foreach ($cList as $c) {
            if ($c -> nodeType != XML_ELEMENT_NODE) {
                $firstName .= $c -> textContent;
                //DOMEntityReference
                //Ausname z.B. <name type="absender">Philar&egrave;te <name type="lastname">Chasles</name></name>
            } else if ($c -> tagName == 'name' && $c -> getAttribute('type') == 'lastname') {
                //zweite vorname
                if (!is_null($lastName) && !is_null($firstName)) {
                    // "und" entfernen
                    $firstName = ltrim(trim($firstName), 'und ');

                }
                $lastName = $c -> textContent;
                if (!is_null($firstName)) {
                    $data = array();
                    $data['name'] = $fullName;
                    $data['firstname'] = $firstName;
                    $data['lastname'] = $lastName;
                    $data['letter_id'] = $letter_id;

                    $model -> add($this->trimData($data));
                    $firstName = '';
                }
            } else {
                $firstName = '';
                //kommt fast nie vor
            }
        }

        if (is_null($lastName)) {
            $data = array();
            $data['name'] = $fullName;
            $data['letter_id'] = $letter_id;
            $model -> add($this->trimData($data));
        }

    }

    private function trimData($data) {
        foreach ($data as $k => $v) {
            $data[$k] = trim($v);
        }
        return $data;
    }

    private function readNode(DOMElement $elem, $data) {
        $tagName = $elem -> tagName;

        $text = null;
        if ($tagName == 'name') {
            $type = $elem -> getAttribute('type');
            $text = $elem -> textContent;
            switch($type) {
                case 'absender' :
                    $data['absender'] = $text;
                    $this -> addToTable($elem, $data['letter_id'], $type);
                    break;
                case 'adressat' :
                    $data['adressat'] = $text;
                    $this -> addToTable($elem, $data['letter_id'], $type);
                    break;
                case 'place' :
                    if (empty($data['adressat'])) {
                        $data['abs_ort'] = $text;
                    } else {
                        $data['adr_ort'] = $text;
                    }
                    break;
            }
        } else if ($tagName == 'date') {
            $rend = $elem -> getAttribute('rend');
            $n = $elem -> getAttribute('n');
            switch($rend) {
                case 'datum' :
                    if (!empty($n)) {
                        $data['tag'] = substr($n, 0, 2);
                        $data['monat'] = substr($n, 2, 2);
                        $data['jahr'] = substr($n, 4);
                    }
                    break;
                case 'day' :
                    if (!empty($n)) {
                        $data['day_n'] = $n;
                    }
                    break;
            }
        } else if ($tagName == 'ref') {
            $type = $elem -> getAttribute('type');
            $text = $elem -> textContent;
            switch($type) {
                case 'kategorie' :
                    $data['kategorie_rend'] = $elem -> getAttribute('rend');
                    $data['kategorie'] = $text;
                    break;
                case 'faksimile' :
                    $data['faksimile'] = $text;
                    $data['faksimile_n'] = $elem -> getAttribute('n');
                    break;
                case 'bemerkung' :
                    $data['bemerkung'] = $text;
                    break;
                case 'status' :
                    $data['status'] = $text;
                    break;
                case 'prev' :
                    $data['prev'] = $elem -> getAttribute('target');
                    break;
                case 'next' :
                    $data['next'] = $elem -> getAttribute('target');
                    break;
            }
        } else if ($tagName == 'lb') {
            $n = $elem -> getAttribute('n');
            if (!empty($n)) {
                $data['lb_n'] = $n;
            }
        } else if ($tagName == 'label' && $elem -> getAttribute('rend') == 'briefnr') {
            $data['briefnr'] = $elem -> textContent;
        }

        $cList = $elem -> childNodes;
        foreach ($cList as $c) {
            if ($c -> nodeType == 1) {
                $data = $this -> readNode($c, $data);
            }
        }
        return $data;
    }

}
